package com.yxhl.zoume.domain.interactor.updateapp;

import com.yxhl.zoume.data.http.repository.updateapp.UpdateAppRepository;
import com.yxhl.zoume.domain.DomainConstants;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;

import javax.inject.Inject;
import javax.inject.Named;

import okhttp3.ResponseBody;
import rx.Scheduler;
import rx.Subscription;
import rx.subscriptions.Subscriptions;

/**
 * Created by sugarya on 16/5/13.
 */
public class UpdateAppUseCase {

    // the uiThread to perform observation actions on
    protected final Scheduler uiThread;
    // the executorThread to perform subscription actions on
    protected final Scheduler executorThread;
    private UpdateAppRepository updateAppRepository;
    private UpdateAppRepository updateAppRepository1;
    private Subscription subscription = Subscriptions.empty();


    @Inject
    public UpdateAppUseCase(
            @Named(DomainConstants.UI_THREAD) Scheduler uiThread,
            @Named(DomainConstants.EXECUTOR_THREAD) Scheduler executorThread,
            UpdateAppRepository updateAppRepository) {
        this.uiThread = uiThread;
        this.executorThread = executorThread;
        this.updateAppRepository = updateAppRepository;
    }


    public void downloadApp(String fileUrl,ZMSubscriber<ResponseBody> zmSubscriber) {
        subscription = updateAppRepository1
                .downloadApp(fileUrl)
                .subscribeOn(executorThread)
                .observeOn(executorThread).subscribe(zmSubscriber);
    }




    public void unsubscribe() {
        if (!subscription.isUnsubscribed())
            subscription.unsubscribe();
    }
}
